/*
 * Reset.h : datatypes to read NBT files
 *
 * Written by T.Pierron, Oct 2010.
 */

#ifndef	NBT_LIB_H
#define	NBT_LIB_H

#include <zlib.h>
#include <stdint.h>
#include "UtilityLib.h"

typedef struct NBT_t *          NBT;
typedef struct Item_t *         Item;
typedef struct ZStream_t *      ZStream;

NBT    NBT_Parse(STRPTR path);
NBT    NBT_GetFromRegion(STRPTR path, STRPTR file, int offset);
NBT    NBT_FindNode(NBT root, STRPTR name);
Bool   NBT_ConvertToInt(NBT array, int * out_array, int nb);
Bool   NBT_SetArrayValue(NBT node, int i, int val);
void   NBT_Save(NBT root, STRPTR path);
void   NBT_Dump(NBT root, int level);
void   NBT_Free(NBT root);
STRPTR NBT_FindItemName(int id);
STRPTR NBT_WoolColor(int id);
STRPTR NBT_ToString(NBT root, STRPTR id, STRPTR def);
int    NBT_ToInt(NBT root, STRPTR id, int def);

struct NBT_t
{
	ListNode node;
	ListHead child;
	NBT      parent;
	STRPTR   name;
	int      type;
	int      len;
	union {
		int8_t  byte;
		int16_t word;
		int32_t dword;
		int64_t qword;
		float   real32;
		double  real64;
		STRPTR  string;
		APTR *  array;
	} val;
};

/* Possible values for NBT.type */
#define	TAG_Byte             1
#define	TAG_Short            2
#define	TAG_Int              3
#define	TAG_Long             4
#define	TAG_Float            5
#define	TAG_Double           6
#define	TAG_Byte_Array       7
#define	TAG_String           8
#define TAG_List             9
#define TAG_Compound         10
#define	TAG_List_Compound    (TAG_List | (TAG_Compound << 8))

struct Item_t
{
	int    id;
	STRPTR name;
};

struct ZStream_t
{
	FILE *   in;
	int      type, remain;
	gzFile   gzin;
	DATA8    bout, bin;
	int      read;
	z_stream strm;
};

#define	DWORD(in)     (((((gzGetC(in) << 8) | gzGetC(in)) << 8) | gzGetC(in)) << 8) | gzGetC(in)
#define	WORD(in)      (gzGetC(in) << 8) | gzGetC(in)

#endif
